﻿NSMobile.models.Order = Ext.regModel('Order', {
    fields:
        [
            { name: 'id', type: 'int' },
            'name',
            'type',
            { name: 'date', type: 'date', dateFormat: 'c', convert: function (value, record) {
                var dateArr;
                if (!value)
                    return;
                
                if (value.indexOf('\/') > -1) {
                    dateArr = value.split('\/');
                    var year = dateArr[2];
                    var month = dateArr[0] - 1;
                    var day = dateArr[1];
                }
                else {
                    dateArr = value.split('-');
                    var year = dateArr[0];
                    var month = dateArr[1];
                    var day = dateArr[2].substring(0, 2).replace('T', '');
                }
                
                return new Date(year, month, day);
            } 
            },
            { name: 'total', type: 'float' },
            { name: 'prv', type: 'float' },
            { name: 'pwv', type: 'float' },
            'period',
            { name: 'number', type: 'int' },
            'status',
            'completedate',
            'commissiondate',
            'partydate',
            'partyenddate',
            'hostess',
            'address',
            { name: 'partydetail', type: 'PartyDetail' },
            'trackingnumbers',
            'trackingurls'
        ]
});

NSMobile.stores.kpiorders = new Ext.data.Store({ //stub for kpi orders as it requires a store
    model: 'Order',
    proxy: {
        type: 'memory',
        reader: {
            type: 'json'
        },
        timeout: ajaxTimeout
    },
    listeners: {
        load: {
            fn: function () {
                NSMobile.stores.kpiorders.sorters.clear();
                if (prvSortPicker) {
                    var values = prvSortPicker.getValue();
                    NSMobile.stores.kpiorders.sort(values.property.toLowerCase(), values.direction);
                }
                else
                    NSMobile.stores.kpiorders.sort('date', 'ASC');
            }
        }
    }
});

NSMobile.stores.orders = new Ext.data.JsonStore({
    model: 'Order',
    proxy: {
        type: 'ajax',
        url: serviceURI + '/GetOrders',
        listeners: {
            exception: {
                fn: function (proxy, response, operation) {
                    showErrorOnce();
                }
            }
        },
        timeout: ajaxTimeout
    },
    listeners: {
        beforeload: {
            fn: function () {
                setSyncIcon('shop3', true);
            }
        },
        load: {
            fn: function (store) {
             //   store.snapshot = store.data; //workaround sencha bug causing filterBy to clear the store
//                var storageData = getStorage('orders');
//                storageData.orders = storageData.orders || {};
//                var orders = {};
//                for (var i = 0; i < store.data.length; i++) {
//                    var order = store.getAt(i);
//                    orders[order.data.id] = order.data;
//                }
//                Ext.apply(storageData.orders, orders);
//                storageData.lastUpdate = new Date();
//                setStorage('orders', storageData);
                ordersLoaded = true;
                //NSMobile.stores.local.orders.load();
               // store.snapshot = store.data; 
//                NSMobile.stores.orders.filterBy(function (record, id) {
//                    var orderDate = new Date(record.data.date);
//                    var filterDate = new Date();
//                    return orderDate.getYear() == filterDate.getYear() && orderDate.getMonth() == filterDate.getMonth();
//                });
                NSMobile.stores.orders.sorters.clear();
                if (ordersSortPicker) {
                    var values = ordersSortPicker.getValue();
                    NSMobile.stores.orders.sort(values.property.toLowerCase(), values.direction);
                }
                else
                    NSMobile.stores.orders.sort('date', 'ASC');
                setSyncIcon('shop3', false);
            }
       }
    }
});

//NSMobile.stores.local.orders = new Ext.data.Store({
//    model: 'Order',
//    proxy: {
//        type: 'memory',
//        reader: {
//            type: 'json'
//        },
//        read: function (operation, callback, scope) {
//            var storageData = getStorage('orders');
//            var orders = [];

//            for (var key in storageData.orders)
//                orders.push(new this.model(storageData.orders[key]));

//            if (!ordersLoaded) {
//                if (orders.length > 0)
//                    NSMobile.stores.orders.getProxy().extraParams.lastUpdate = storageData.lastUpdate;
//                NSMobile.stores.orders.load();
//                NSMobile.stores.orders.getProxy().extraParams.lastUpdate = undefined;
//            }

//            operation.resultSet = new Ext.data.ResultSet({
//                records: orders,
//                total: orders.length,
//                loaded: true
//            });
//            operation.setSuccessful();
//            operation.setCompleted();

//            if (typeof callback == 'function') {
//                callback.call(scope || this, operation);
//            }
//        }
//    },
//    listeners: {
//        load: {
//            fn: function (store) {
//                store.snapshot = store.data; //workaround sencha bug causing filterBy to clear the store
//                NSMobile.stores.local.orders.filterBy(function (record, id) {
//                    var orderDate = new Date(record.data.date);
//                    var filterDate = new Date();
//                    return orderDate.getYear() == filterDate.getYear() && orderDate.getMonth() == filterDate.getMonth();
//                });
//                NSMobile.stores.local.orders.sorters.clear();
//                if (ordersSortPicker) {
//                    var values = ordersSortPicker.getValue();
//                    NSMobile.stores.local.orders.sort(values.property.toLowerCase(), values.direction);
//                }
//                else
//                    NSMobile.stores.local.orders.sort('date', 'ASC');
//            }
//        }
//    }
//});

NSMobile.stores.partyorders = new Ext.data.JsonStore({
    model: 'Order',
    proxy: {
        type: 'ajax',
        url: serviceURI + '/GetPartyOrders',
        timeout: ajaxTimeout
    },
    listeners: {
        load: {
            fn: function (store, records, successful) {
                if (records.length == 1) {
                    var data = records[0].data;
                    Ext.getCmp('partyinfo').update(data);
                    Ext.getCmp('partydetails').update(data);
                    Ext.getCmp('partylandingtoolbar').setTitle(data.name);
                    Ext.getCmp('parties').setActiveItem(1);
                }
            }
        }
    }
});

var ordersLoaded = false;